<?php
if (!defined("MCTDESIGN")) die("# ");
if (isset($_POST['delete']) || isset($flag) && $flag == 1) {
	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;
 	for($i=1;$i<=$total;$i++){
		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';
		if (!empty($checkbox[$i])) {
		      $sli_del_id = $checkbox[$i];
			  $anyDB->query("DELETE FROM ".$tb_fix."_slideshow WHERE slide_id='$sli_del_id'");
		}
	}
	$html_body = _html_trans("Đã xoá video thành công !");
	return false;
}
function reArrayFiles($file_post) {
    // sap xep lai mang $_FILE khi su dung multi upload
    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);
    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }
    return $file_ary;
}
function check_empty_files_array($files){
    foreach ($files["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            return true;
        }else{
            return false;
        }
    }
}

if (isset($_POST['add'])) {
	$y_title = isset($_POST["y_title"]) ? fixquotes(stripslashes($_POST["y_title"])) : "";    
    $y_video = isset($_FILES['y_video']['name'] ) ? $_FILES['y_video']['name'] : '';
	$y_video_temp = isset($_FILES['y_video']['tmp_name'] ) ? $_FILES['y_video']['tmp_name'] : '';
    $y_status = isset($_POST["y_status"]) ? intval($_POST["y_status"]) : 2;
    $f_url = isset($_POST["f_url"]) ? fixquotes(stripslashes($_POST["f_url"])) : "";
    $f_where = isset($_POST["f_where"]) ? fixquotes(stripslashes($_POST["f_where"])) : "";
    //$f_img = isset($_FILES['f_img']['name'] ) ? $_FILES['f_img']['name'] : '';
	//$f_img_temp = isset($_FILES['f_img']['tmp_name'] ) ? $_FILES['f_img']['tmp_name'] : '';
    $isVideo = $_POST['isVideo'];
    $isFlash = $_POST['isFlash'];
    $isPartners = $_POST['isPartners'];
    $isShowrooms = $_POST['isShowrooms'];
    $isCertificate = $_POST['isCertificate'];
    if($isVideo){
        if(!isset($_POST['isEditVideo'])){
            if(empty($y_video) || empty($y_title)){
               $html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");    
		        return false; 
            }
        }else{
            if(empty($y_title)){
               $html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");    
		        return false; 
            }
        }
        /*if($y_status == 1){
            $anyDB->query(QueryHelper::update($tb_fix."_slideshow","slide_status=2","slide_ndsgroup=2"));
        }*/
        $y_video_uploaded = uploadIMG($y_video,$y_video_temp,"logos");        
        $q = array(
            'slide_name'=>"$y_title",
            'slide_url'=>"$y_video_uploaded",
            'slide_status'=>"$y_status",
            'slide_ndsgroup'=>"2"
        );
        $sql = QueryHelper::insert($tb_fix."_slideshow",$q); 
        $html = "Đã thêm Video thành công!";
        //VIDEO EDIT
        if(isset($_POST['isEditVideo'])){
            $video_id = (int)$_POST['video_id'];
            if(empty($y_video)){
                $q = array(
                'slide_name'=>"$y_title",                
                'slide_status'=>"$y_status",
                'slide_ndsgroup'=>"2"
                );    
            }else{
                // xoa file cu
                $r = $anyDB->getNext($anyDB->query(QueryHelper::select("slide_url",$tb_fix."_slideshow","slide_id = '$video_id'")));
                @unlink($r['slide_url']);
                // upload file moi
                $y_video_uploaded = uploadIMG($y_video,$y_video_temp,"logos"); 
                $q = array(
                'slide_name'=>"$y_title",
                'slide_url'=>"$y_video_uploaded",
                'slide_status'=>"$y_status",
                'slide_ndsgroup'=>"2"
                );    
            }
            
            $sql = QueryHelper::update($tb_fix."_slideshow",$q,"slide_id = '$video_id'"); 
            //echo $sql;die();
            $html = "Đã sửa Video thành công!";
        };   
    }    
    //FLASH
    if($isFlash){
            if (empty($f_url) || !check_empty_files_array($_FILES['f_img'])) {    
        		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");    
        		return false;        
            }
            $file_ary = reArrayFiles($_FILES['f_img']);
            foreach ($file_ary as $file) {
                $fileName =  $file['name'];
                $fileTmpName = $file['tmp_name'];
                if(!empty($fileName)){
                    $post_img = @uploadIMG($fileName,$fileTmpName,"logos");
                    $q_add_flash = array(
                        'slide_url'=>"$f_url",
                        'slide_where'=>"$f_where",
                        'slide_img' => "$post_img",
                        'slide_ndsgroup'=>"1"
                    );
                    $sql_add_flash = QueryHelper::insert($tb_fix."_slideshow",$q_add_flash);
                    $anyDB->query($sql_add_flash);
                }
            }
            
            $html = "Đã thêm Flash thành công!";
    }
    //Partners
    if($isPartners){
            if (empty($f_url) || !check_empty_files_array($_FILES['f_img'])) {    
        		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");    
        		return false;        
            }
            $file_ary = reArrayFiles($_FILES['f_img']);
            foreach ($file_ary as $file) {
                $fileName =  $file['name'];
                $fileTmpName = $file['tmp_name'];
                if(!empty($fileName)){
                    $post_img = @uploadIMG($fileName,$fileTmpName,"logos");
                    $q_add_partners = array(
                        'slide_url'=>"$f_url",
                        'slide_where'=>"partners",
                        'slide_img' => "$post_img",
                        'slide_ndsgroup'=>"0"
                    );
                    $sql_add_partners = QueryHelper::insert($tb_fix."_slideshow",$q_add_partners);
                    $anyDB->query($sql_add_partners);
                }
            }
            
            $html = "Đã thêm đối tác thành công!";
    }
    //Showrooms
    if($isShowrooms){
            if (empty($f_url) || !check_empty_files_array($_FILES['f_img'])) {    
        		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");    
        		return false;        
            }
            $file_ary = reArrayFiles($_FILES['f_img']);
            foreach ($file_ary as $file) {
                $fileName =  $file['name'];
                $fileTmpName = $file['tmp_name'];
                if(!empty($fileName)){
                    $post_img = @uploadIMG($fileName,$fileTmpName,"logos");
                    $q_add_showrooms = array(
                        'slide_url'=>"$f_url",
                        'slide_where'=>"showrooms",
                        'slide_img' => "$post_img",
                        'slide_ndsgroup'=>"0"
                    );
                    $sql_add_showrooms = QueryHelper::insert($tb_fix."_slideshow",$q_add_showrooms);
                    $anyDB->query($sql_add_showrooms);
                }
            }
            
            $html = "Đã thêm showrooms thành công!";
    }
    //Certificate
    if($isCertificate){
            if (empty($f_url) || !check_empty_files_array($_FILES['f_img'])) {    
        		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");    
        		return false;        
            }
            $file_ary = reArrayFiles($_FILES['f_img']);
            foreach ($file_ary as $file) {
                $fileName =  $file['name'];
                $fileTmpName = $file['tmp_name'];
                if(!empty($fileName)){
                    $post_img = @uploadIMG($fileName,$fileTmpName,"logos");
                    $q_add_certificate = array(
                        'slide_url'=>"$f_url",
                        'slide_where'=>"certificate",
                        'slide_img' => "$post_img",
                        'slide_ndsgroup'=>"0"
                    );
                    $q_add_certificate = QueryHelper::insert($tb_fix."_slideshow",$q_add_certificate);
                    $anyDB->query($q_add_certificate);
                }
            }
            
            $html = "Đã thêm chứng chỉ thành công!";
    }
    $anyDB->query($sql);	
	$html_body = _html_trans($html);
	return false;
}

function _html_video(){
   global $dir_tpl,$tb_fix,$temp,$anyDB;
	$temp_body = $temp;
    $pagegroup_size = 5;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>2) {$limit=10;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    //dem so ban gi 
    $sql ="Select COUNT(slide_id) as tongbanghi from ".$tb_fix."_slideshow where slide_ndsgroup = 2";
    $q = $anyDB->query($sql);
    $arr = $anyDB->getNext($q);
    $totalRecord = $arr['tongbanghi']; // tong so ban ghi
    
    $sql = "SELECT * FROM ".$tb_fix."_slideshow WHERE slide_ndsgroup = 2 ORDER BY slide_status ASC $li";
    $q = $anyDB->query($sql);
    $t = $anyDB->numRows($q); 
    $dem_ban_ghi = 0;
    if($page==1) $dem_ban_ghi = ($limit-$limit)+1;
    if($page>=2) $dem_ban_ghi = ($page*$limit)-$limit+1;
    //if($page>2) $dem_ban_ghi = ($page*$limit)-$limit;   
    if($t>0){
        $row = $anyDB->getAll($q);        
        $temp_body->assign("thutu",$dem_ban_ghi);
        $temp_body->assign("you",$row);
        $getURL = getURLPaging();
        $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"$getURL");
        $temp_body->assign('pading',$pages);
    }    
    $temp_body->assign("base_tpl",temp_default());
    $temp_body->assign("dir_tpl",$dir_tpl);    
    $temp_body->assign("breakcum",breakcum("Quản lý video  "));
	$_html = $temp_body->fetch( '_adm_video.mct' );

	return $_html; 
}
function _html_flash(){
    global $dir_tpl, $tb_fix,$temp,$anyDB;
    $temp_body = $temp;
    $f_group = isset($_POST["f_group"]) ? fixquotes(stripslashes($_POST["f_group"])) : "trangchu";
    if($f_group){
        $sql_g = "and slide_where='$f_group'";   
    }	
    $sql = "SELECT * FROM ".$tb_fix."_slideshow WHERE slide_ndsgroup = 1 $sql_g ORDER BY slide_id DESC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp_body->assign("fla",$row);
    $temp_body->assign("g",$f_group);    
    $temp_body->assign("dir_tpl",$dir_tpl);  
    $temp_body->assign("base_tpl",temp_default());
    $temp_body->assign("breakcum",breakcum("Quản lý flash  "));
	$_html = $temp_body->fetch( '_adm_flash.mct' );

	return $_html; 
}
function _html_partners(){
    global $dir_tpl, $tb_fix,$temp,$anyDB;
    $temp_body = $temp;
    $sql = "SELECT * FROM ".$tb_fix."_slideshow WHERE slide_ndsgroup = 0 AND slide_where = 'partners' ORDER BY slide_id DESC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp_body->assign("fla",$row);
    $temp_body->assign("g",$f_group);    
    $temp_body->assign("dir_tpl",$dir_tpl);  
    $temp_body->assign("base_tpl",temp_default());
    $temp_body->assign("breakcum",breakcum("Quản lý đối tác  "));
	$_html = $temp_body->fetch( '_adm_partners.mct' );

	return $_html; 
}
function _html_showrooms(){
    global $dir_tpl, $tb_fix,$temp,$anyDB;
    $temp_body = $temp;
    $sql = "SELECT * FROM ".$tb_fix."_slideshow WHERE slide_ndsgroup = 0 AND slide_where = 'showrooms' ORDER BY slide_id DESC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp_body->assign("fla",$row);
    $temp_body->assign("g",$f_group);    
    $temp_body->assign("dir_tpl",$dir_tpl);  
    $temp_body->assign("base_tpl",temp_default());
    $temp_body->assign("breakcum",breakcum("Quản lý Showrooms  "));
	$_html = $temp_body->fetch( '_adm_showrooms.mct' );

	return $_html; 
}
function _html_certificate(){
    global $dir_tpl, $tb_fix,$temp,$anyDB;
    $temp_body = $temp;
    $sql = "SELECT * FROM ".$tb_fix."_slideshow WHERE slide_ndsgroup = 0 AND slide_where = 'certificate' ORDER BY slide_id DESC";
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp_body->assign("fla",$row);
    $temp_body->assign("g",$f_group);    
    $temp_body->assign("dir_tpl",$dir_tpl);  
    $temp_body->assign("base_tpl",temp_default());
    $temp_body->assign("breakcum",breakcum("Quản lý Chứng chỉ  "));
	$_html = $temp_body->fetch( '_adm_certificate.mct' );

	return $_html; 
}
//NAVIGATION
$go = $_GET['go'];
switch($go){
    case "video":
        $html_body = _html_video();
        break;
    case "flash":
        $html_body = _html_flash();
        break;
    case "partners":
        $html_body = _html_partners();
        break;      
    case "showrooms":
        $html_body = _html_showrooms();
        break;
    case "certificate":
        $html_body = _html_certificate();
        break;        
    default:
        $html_body = _html_video();
        break;      
}
?>